package com.acwing.partition43;

import java.io.*;
import java.util.HashSet;
import java.util.Set;

/**
 * @author `RKC`
 * @date 2022/6/16 9:43
 */
public class AC4269校庆 {

    private static final int N = 100010;
    private static Set<String> set = new HashSet<>(N);
    private static String maxId = "999999999999999999", maxSchool = maxId;
    private static int n, m;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        for (int i = 0; i < n; i++) set.add(reader.readLine());
        m = Integer.parseInt(reader.readLine());
        int cnt = 0;
        for (int i = 0; i < m; i++) {
            String id = reader.readLine();
            if (compare(maxId, id) > 0) maxId = id;
            if (set.contains(id)) {
                cnt++;
                if (compare(maxSchool, id) > 0) maxSchool = id;
            }
        }
        writer.write(cnt + "\n");
        if (cnt != 0) writer.write(maxSchool);
        else writer.write(maxId);
        writer.flush();
    }

    private static int compare(String id1, String id2) {
        String s1 = id1.substring(6, 14), s2 = id2.substring(6, 14);
        int y1 = Integer.parseInt(s1.substring(0, 4)), y2 = Integer.parseInt(s2.substring(0, 4));
        int m1 = Integer.parseInt(s1.substring(4, 6)), m2 = Integer.parseInt(s2.substring(4, 6));
        int d1 = Integer.parseInt(s1.substring(6)), d2 = Integer.parseInt(s2.substring(6));
        if (y1 != y2) return y1 - y2;
        if (m1 != m2) return m1 - m2;
        if (d1 != d2) return d1 - d2;
        return 0;
    }
}
